<?php
/* Reminder: always indent with 4 spaces (no tabs). */



// +---------------------------------------------------------------------------+
// | 機能  XMLを読み込みデータ出力
// | 書式 fncimportexec ()                                                     |
// +---------------------------------------------------------------------------+
// | 戻値 nomal:
// +---------------------------------------------------------------------------+
//fncimportexec2
function assist_xmlimport(
    $pi_name
    ,$importfile
    ,$defxml
)
{
	$retval['ok']=0;
	$retval['ng']=0;
	$retval['msg']="";

    global $_CONF;
    global $_TABLES;
    global $_ASSIST_CONF;

    // XMLファイルの読込み
    $content=@file_get_contents($importfile);
    if ($content==FALSE){
		$retval['ng']=1;
        $retval['msg'] = "Error ".$importfile." file error!";
        return  $retval;
    }


    // XMLデータを配列に格納
    $xml_parser=xml_parser_create();
    xml_parse_into_struct($xml_parser,$content,$vals);
    xml_parser_free($xml_parser);

    $vals2=array();
    for ($i = 1; $i <= count($vals); $i++) {
        $w=current($vals);
        if ($w[tag]<>"ROOT"){
            $vals2[$w[tag]]=$w[value];
        }
        next($vals);
    }


    $maps=array();

    $cnt=count($defxml);

    for ($i = 0; $i < $cnt; $i++) {

        $tag=    $defxml[$i]['tag'];
        $value=  $defxml[$i]['value'];
        $type =  $defxml[$i]['type'];
        $field=  $defxml[$i]['field'];

        //maps項目
        if ($type==="maps"){

            $maps[$field]=$vals2[$tag];
        }
    }


    if (count($maps)>0){
        //INSERT
        $rt=assist_xmlimportmaps($maps);
		$retval['ok']=$rt['ok'];
		$retval['ng']=$rt['ng'];
		$retval['msg'].=$rt['msg'];
    }

    return $retval;
}




//MapsMerkar Insert
//fncInsertMaps
function assist_xmlimportmaps(
    $maps
    )
{
	$retval['ok']=0;
	$retval['ng']=0;
	$retval['msg']="";

    global $_CONF;
    global $_TABLES;

    global $_MAPS_CONF;
    global $_USER;
    global $_GROUPS;

    global $LANG_ASSIST_ADMIN;


    $group_id=$_GROUPS['Maps Admin'];

    $array = array();
    SEC_setDefaultPermissions($array, $_MAPS_CONF['default_permissions']);
    $perm_owner = $array['perm_owner'];
    $perm_group = $array['perm_group'];
    $perm_anon = $array['perm_anon'];
    $perm_members = $array['perm_members'];


    $pi_name="assist";
    $err="";

    //-----
    $name= $maps['name'];
    if ($name==""){
        $err.=$LANG_ASSIST_ADMIN['err_marker_name'];
    }
    $address = $maps['address'];
    if ($address==""){
        $err.=$LANG_ASSIST_ADMIN['err_marker_address'];
    }
	$lat=$maps['lat'];
	$lng=$maps['lng'];

	// prepare strings for insertion
	$created = date("YmdHis");
	$modified = date("YmdHis");

    // lat, lng can only contain numbers and a decimal
	if (empty($lat) || empty($lng)) {
	    $coords = MAPS_getCoords($address, $lat, $lng);
		if ($lat == 0 && $lng == 0) {
            $err.=$LANG_ASSIST_ADMIN['err_marker_coords'];
		}
	}

    //errorのあるとき
	if ($err<>"") {
		$retval["ng"]=1;
		$retval["msg"]=$err;
        return $retval;

    }

    $newmkid = addslashes (COM_makeSid ());

    $mid=DB_getItem($_TABLES['maps_maps'], 'mid');
	if ($mid=="") {
		$retval["ng"]=1;
		$retval["msg"]=$LANG_ASSIST_ADMIN['err_map'];
        return $retval;

	}

    if ($maps['mname']<>""){//マップ名
        $mid=DB_getItem($_TABLES['maps_maps'], 'mid',"name='{$maps['mname']}'");
    	if ($mid=="") {
			$retval["ng"]=1;
			$retval["msg"]=$LANG_ASSIST_ADMIN['err_map'];
			return $retval;
    	}
    }


    $validity=$_MAPS_CONF['map_hidden'];//有効期限
    $active=$_MAPS_CONF['map_active'];
    $payed=$_MAPS_CONF['payed'];//ペイドマーカー

    $description=$maps['description'];//詳細
    $remark=$maps['remark'];//注意
    $street=$maps['street'];//番地
    $city=$maps['city'];//市
    $code=$maps['code'];//〒
    $state=$maps['state'];//都道府県
    $country=$maps['country'];//国
    $tel=$maps['tel'];//TEL
    $fax=$maps['fax'];//FAX
    $web=$maps['web'];//WEB
    $item_1=$maps['item_1'];//リソース #1
    $item_2=$maps['item_2'];//リソース #2
    $item_3=$maps['item_3'];//リソース #3
    $item_4=$maps['item_4'];//リソース #4
    $item_5=$maps['item_5'];//リソース #5
    $item_6=$maps['item_6'];//リソース #6
    $item_7=$maps['item_7'];//リソース #7
    $item_8=$maps['item_8'];//リソース #8
    $item_9=$maps['item_9'];//リソース #9
    $item_10=$maps['item_10'];//リソース #10

    $owner_id=$_USER['uid'];
    $group_id=$_GROUPS['Maps Admin'];

    $array = array();
    SEC_setDefaultPermissions($array, $_MAPS_CONF['default_permissions']);
    $perm_owner = $array['perm_owner'];
    $perm_group = $array['perm_group'];
    $perm_anon = $array['perm_anon'];
    $perm_members = $array['perm_members'];


    //-------
    $fields="mkid".LB;
    $values="'$newmkid'".LB;

    $fields.=",name".LB;
    $values.=",'$name'".LB;

    $fields.=",description".LB;
    $values.=",'$description'".LB;

    $fields.=",created".LB;
    $values.=",'$created'".LB;

    $fields.=",modified".LB;
    $values.=",'$modified'".LB;

    $fields.=",payed".LB;
    $values.=",'$payed'".LB;

    $fields.=",validity".LB;
    $values.=",'$validity'".LB;

    $fields.=",active".LB;
    $values.=",'$active'".LB;


    $fields.=",address".LB;
    $values.=",'$address'".LB;

    $fields.=",lat".LB;
    $values.=",'$lat'".LB;

    $fields.=",lng".LB;
    $values.=",'$lng'".LB;

    $fields.=",mid".LB;
    $values.=",'$mid'".LB;

    $fields.=",remark".LB;
    $values.=",'$remark'".LB;

    $fields.=",street".LB;
    $values.=",'$street'".LB;

    $fields.=",city".LB;
    $values.=",'$city'".LB;

    $fields.=",code".LB;
    $values.=",'$code'".LB;

    $fields.=",state".LB;
    $values.=",'$state'".LB;

    $fields.=",country".LB;
    $values.=",'$country'".LB;

    $fields.=",tel".LB;
    $values.=",'$tel'".LB;

    $fields.=",fax".LB;
    $values.=",'$fax'".LB;

    $fields.=",web".LB;
    $values.=",'$web'";

    $fields.=",item_1".LB;
    $values.=",'$item_1'".LB;
    $fields.=",item_2".LB;
    $values.=",'$item_2'".LB;
    $fields.=",item_3".LB;
    $values.=",'$item_3'".LB;
    $fields.=",item_4".LB;
    $values.=",'$item_4'".LB;
    $fields.=",item_5".LB;
    $values.=",'$item_5'".LB;
    $fields.=",item_6".LB;
    $values.=",'$item_6'".LB;
    $fields.=",item_7".LB;
    $values.=",'$item_7'".LB;
    $fields.=",item_8".LB;
    $values.=",'$item_8'".LB;
    $fields.=",item_9".LB;
    $values.=",'$item_9'".LB;
    $fields.=",item_10".LB;
    $values.=",'$item_10'".LB;

    $fields.=",owner_id".LB;
    $values.=",'$owner_id'".LB;

    $fields.=",group_id".LB;
    $values.=",'$group_id'".LB;

    $fields.=",perm_owner".LB;
    $values.=",'$perm_owner'".LB;

    $fields.=",perm_group".LB;
    $values.=",'$perm_group'".LB;

    $fields.=",perm_members".LB;
    $values.=",$perm_members".LB;

    $fields.=",perm_anon".LB;
    $values.=",$perm_anon".LB;

    $fields.=",submission".LB;
    $values.=",0".LB;
	
	//事前削除
	$rt=assist_deletemapsmarker("item_10",$item_10);
	
	//追加
    updateMap($mid);
    DB_save($_TABLES['maps_markers'],$fields,$values);


    //$dummy=fncsendmail ('data',$id);
	
	$retval["ok"]=1;
    $retval["msg"]="Maps marker Insert id: ".$newmkid." name:".$name;

    return $retval;
}

function assist_deletemapsmarker(
	$codename
	,$code
)
{
	if ($code==""){
		return;
	}
	
	global $_TABLES;
	global $LANG_MAPS_1;
	
	$mkid=DB_getItem( $_TABLES['maps_markers']   ,"mkid","{$codename}='{$code}'");
	if ($mkid<>""){
		DB_delete($_TABLES['maps_markers'], 'mkid', $mkid);
		if (DB_affectedRows('') == 1) {
			$msg = $LANG_MAPS_1['deletion_succes'];
		} else {
			$msg = $LANG_MAPS_1['deletion_fail'];
		}
	}
}
function assist_GetDefXml(
    $pi_name
)
{
    global $_TABLES;

    //-----
    $tbl1=$_TABLES[strtoupper($pi_name).'_def_xml'];

    //-----
    $sql = "SELECT ".LB;
    $sql .= " tag ".LB;
    $sql .= " ,value ".LB;
    $sql .= " ,field ".LB;
    $sql .= " ,seq ".LB;
    $sql .= " FROM ".LB;
    $sql .= " {$tbl1} AS t1 ".LB;
    $sql .= " ORDER BY seq ".LB;
    $result = DB_query ($sql);
    $cnt = DB_numRows ($result);

    $result = DB_query ($sql);
    $numrows = DB_numRows ($result);

    $defxml=array();
    if ($numrows > 0) {
        for ($i = 0; $i < $numrows; $i++) {
            $A = DB_fetchArray ($result);

            $defxml[$i]['tag']=COM_applyFilter($A['tag']);
            $defxml[$i]['value']=COM_applyFilter($A['value']);
            $field=COM_applyFilter($A['field']);
			
			$fld=explode(":",$field);
            $defxml[$i]['type']=$fld[0];
            $defxml[$i]['field']=$fld[1];

        }
    }

    return $defxml;

}


?>
